Completed
Pull Request — master (#117)
by Sander
46s
created

angular.controller(ꞌSetupCtrlꞌ)   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 47
rs 9.0303
c 2
b 0
f 0
1
/* global API */
2
3
/**
4
 * Nextcloud - passman
5
 *
6
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
7
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
(function () {
26
    'use strict';
27
28
    /**
29
     * @ngdoc function
30
     * @name passmanApp.controller:MainCtrl
31
     * @description
32
     * # MainCtrl
33
     * Controller of the passmanApp
34
     */
35
    angular.module('passmanExtension')
36
        .controller('SetupCtrl', ['$scope', '$timeout', '$location', '$rootScope', 'StepsService', 'notify', function ($scope, $timeout, $location, $rootScope, StepsService, notify) {
37
            $scope.settings = {
38
                nextcloud_host: '',
39
                nextcloud_username: '',
40
                nextcloud_password: '',
41
                ignoreProtocol: true,
42
                ignoreSubdomain: true,
43
                ignorePath: true,
44
                generatedPasswordLength: 12,
45
                remember_password: true,
46
                vault_password: '',
47
                refreshTime: 60,
48
                default_vault: {},
49
                master_password: '',
50
                disableAutoFill: false,
51
                disablePasswordPicker: false,
52
                disable_browser_autofill: true,
53
                debug: false,
54
                accounts: []
55
            };
56
            $scope.vaults = [];
57
58
            $rootScope.$broadcast('hideHeader');
59
            $rootScope.setup = true;
60
            $scope.gogo = function (to) {
61
                StepsService.steps().goTo(to);
62
            };
63
            notify.config({
64
                'position': 'left',
65
                'duration': 2500
66
            });
67
68
            $scope.check = {
69
                server: function (callback) {
70
                    if(!$scope.settings.nextcloud_host || !$scope.settings.nextcloud_username || !$scope.settings.nextcloud_password){
71
                        $scope.errors.push(API.i18n.getMessage('invalid_server_settings'));
72
                        callback(false);
73
                        return;
74
                    }
75
                    $scope.settings.nextcloud_host = $scope.settings.nextcloud_host.replace(/\/$/, "");
76
                    PAPI.host = $scope.settings.nextcloud_host;
77
                    PAPI.username = $scope.settings.nextcloud_username;
78
                    PAPI.password = $scope.settings.nextcloud_password;
79
                    PAPI.getVaults(function (vaults) {
80
                        if (vaults.hasOwnProperty('error')) {
81
                            var errors = API.i18n.getMessage('invalid_response_from_server', [vaults.result.status, vaults.result.statusText]);
82
                            $scope.errors.push(errors);
83
                            notify(errors);
84
                            callback(false);
85
                        }
86
                        else {
87
                            $scope.vaults = vaults;
88
                            callback(true);
89
                        }
90
                        $scope.$apply();
91
                    });
92
                },
93
                vault: function (callback) {
94
                    try {
95
                        PAPI.decryptString($scope.settings.default_vault.challenge_password, $scope.settings.vault_password);
96
                        callback(true);
97
                    }
98
                    catch (e) {
99
                        $scope.errors.push();
100
                        notify(API.i18n.getMessage('invalid_vault_password'));
101
                        callback(false);
102
                    }
103
                },
104
                master: function (callback) {
105
                    if ($scope.settings.master_password.trim() !== '') {
106
                        callback(true);
107
                    } else {
108
                        notify(API.i18n.getMessage('empty_master_key'));
109
                        callback(false);
110
                    }
111
                }
112
            };
113
            $scope.saving = false;
114
            $scope.next = function () {
115
                $scope.saving = true;
116
                $scope.errors = [];
117
                $timeout(function () {
118
                    var step = StepsService.getCurrent().name;
119
                    var check = $scope.check[step];
120
                    if (typeof check === "function") {
121
                        check(function (result) {
122
                            $scope.saving = false;
123
                            if (result) {
124
                                $scope.errors = [];
125
                                $scope.$apply();
126
                                StepsService.steps().next();
127
                            }
128
                            $timeout(function () {
129
                                $scope.errors = [];
130
                                $scope.$apply();
131
                            }, 5000);
132
                        });
133
                    }
134
                    else {
135
                        $scope.saving = false;
136
                        StepsService.steps().next();
137
                    }
138
                }, 10);
139
            };
140
141
            $scope.finished = function () {
142
                var settings = angular.copy($scope.settings);
143
                var master_password = settings.master_password;
144
                var master_password_remember = settings.master_password_remember;
145
                var account = {
146
                    nextcloud_host: settings.nextcloud_host,
147
                    nextcloud_username: settings.nextcloud_username,
148
                    nextcloud_password: settings.nextcloud_password,
149
                    vault: settings.default_vault,
150
                    vault_password: settings.vault_password,
151
                };
152
                settings.accounts.push(account);
153
                delete settings.master_password;
154
                delete settings.master_password_remember;
155
                delete settings.nextcloud_host;
156
                delete settings.nextcloud_username;
157
                delete settings.nextcloud_password;
158
                delete settings.vault_password;
159
                delete settings.default_vault;
160
161
                $scope.saving = true;
162
                console.log(settings);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
163
164
165
                API.runtime.sendMessage(API.runtime.id, {
166
                    method: "setMasterPassword",
167
                    args: {password: master_password, savePassword: master_password_remember}
168
                })
169
                    .then(function () {
170
                        API.runtime.sendMessage(API.runtime.id, {
171
                            method: "saveSettings",
172
                            args: settings
173
                        }).then(function () {
174
                            setTimeout(function () {
175
                                $rootScope.setup = false;
176
                                $rootScope.$broadcast('showHeader');
177
                                window.location = '#!/';
178
                                API.runtime.sendMessage(API.runtime.id, {
179
                                    method: "closeSetupTab"
180
                                });
181
                                $scope.saving = false;
182
                            }, 750);
183
                        });
184
                    });
185
186
187
            };
188
        }]);
189
}());
190
191